package com.zkca.xf.ui.activity.technicalprotect;


import android.content.Intent;

import android.os.Bundle;

import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zkca.xf.Constants_SP;
import com.zkca.xf.R;
import com.zkca.xf.base.BaseTitleActivity;
import com.zkca.xf.model.javabean.FireAlarmBean;
import com.zkca.xf.presenter.FireAlarmPresenter;
import com.zkca.xf.ui.activity.baidumap.BaiDuRoutePlanningActivity;
import com.zkca.xf.ui.activity.baidumap.DrivingRouteOverlayAct;
import com.zkca.xf.ui.adapter.SingleAdapter;
import com.zkca.xf.ui.adapter.ViewHolder;
import com.zkca.xf.utils.DisplayUtil;
import com.zkca.xf.utils.LogUtil;
import com.zkca.xf.utils.SPUtil;
import com.zkca.xf.utils.SpaceItemDecoration;
import com.zkca.xf.utils.ToastUtils;
import com.zkca.xf.view.FireAlarmView;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

//火灾报警
public class FireAlarmActivity extends BaseTitleActivity implements FireAlarmView {

    private FireAlarmPresenter fireAlarmPresenter;

    private Map<String, String> map;
    private int numble = 0;
    private List<FireAlarmBean.DataBean> dataList;
    @BindView(R.id.xrv_fa)
    XRecyclerView xrv_fa;
    private LinearLayoutManager linearLayoutManager;
    private SingleAdapter<FireAlarmBean.DataBean> adapter;
    private String userType, user_id, org_id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fire_alarm);
        ButterKnife.bind(this);
        fireAlarmPresenter = new FireAlarmPresenter(this, this);
        initview();
        initdata();

    }

    private void initdata() {

        map = new HashMap<>();
        userType = SPUtil.getString(this,Constants_SP.USERTYPE, "");
        user_id = SPUtil.getString(this,Constants_SP.USER_ID, "");
        org_id = SPUtil.getString(this,Constants_SP.ORG_ID, "");
        if ("XF".equals(userType) || "ADMIN".equals(userType)) {
            map.put("userId", user_id);
        } else {
            map.put("orgId", org_id);
        }
        map.put("page", numble + "");
        map.put("size", "10");
        fireAlarmPresenter.GetFireAlarm(map);
    }

    private void initview() {
        dataList = new ArrayList<>();
        setTitleText("火灾报警");
        setleftTextVisibility(true);
        setleftImgVisibility(true);

        linearLayoutManager = new LinearLayoutManager(mContext);
        xrv_fa.setLayoutManager(linearLayoutManager);
        xrv_fa.addItemDecoration(new SpaceItemDecoration(DisplayUtil.dip2px(
                mContext, 0.5f)));


        xrv_fa.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                dataList.clear();
                numble = 0;
                map = new HashMap<>();
                if ("XF".equals(userType) || "ADMIN".equals(userType)) {
                    map.put("userId", user_id);
                } else {
                    map.put("orgId", org_id);
                }
                map.put("page", numble + "");
                map.put("size", "10");
                fireAlarmPresenter.GetFireAlarm(map);
            }

            @Override
            public void onLoadMore() {
                numble++;
                map = new HashMap<>();
                if ("XF".equals(userType) || "ADMIN".equals(userType)) {
                    map.put("userId", user_id);
                } else {
                    map.put("orgId", org_id);
                }
                map.put("page", numble + "");
                map.put("size", "10");
                fireAlarmPresenter.GetFireAlarm(map);
            }
        });


        adapter = new SingleAdapter<FireAlarmBean.DataBean>(mContext, dataList, R.layout.huozai_item) {
            @Override
            public void convert(ViewHolder holder, FireAlarmBean.DataBean dataBean, int position) {
                TextView jjtime = holder.getView(R.id.jjtime);
                TextView zdname = holder.getView(R.id.zdname);
                TextView zdadss = holder.getView(R.id.zdadss);
                TextView jqleixing = holder.getView(R.id.jqleixing);
                TextView jqnum = holder.getView(R.id.jqnum);
                TextView yhname = holder.getView(R.id.yhname);
                LinearLayout iv_1 = holder.getView(R.id.iv_1);
                LinearLayout iv_2 = holder.getView(R.id.iv_2);
                LinearLayout iv_3 = holder.getView(R.id.iv_3);

                jjtime.setText("接警时间：" + dataBean.getCreateTime());
                zdname.setText("终端编号：" + dataBean.getDeviceId());
                zdadss.setText("终端位置：" + dataBean.getPosition());
                jqleixing.setText("警情描述：" + dataBean.getFireDesc());
                jqnum.setText("警情号码：" + dataBean.getId());
                yhname.setText(dataBean.getOrgName());

                final String jingDu = dataBean.getJingDu();
                final String weiDu = dataBean.getWeiDu();
                final String id = dataBean.getId();
                iv_3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.isEmpty(jingDu) || TextUtils.isEmpty(weiDu)) {
                            ToastUtils.show(FireAlarmActivity.this, "经纬度没有返回值");
                            return;
                        }
                        Intent intent = new Intent(mContext, BaiDuRoutePlanningActivity.class);
                        intent.putExtra("jingdu", jingDu);
                        intent.putExtra("weidu", weiDu);
                        intent.putExtra("id", id);
                        mContext.startActivity(intent);
                    }
                });
                iv_2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
            }
        };
        xrv_fa.setAdapter(adapter);
    }

    @Override
    public void showProgressDialog() {

    }

    @Override
    public void hideProgressDialog() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void getFireAlarm(FireAlarmBean dataBean) {
        try {
            xrv_fa.refreshComplete();
            xrv_fa.loadMoreComplete();
            if (dataBean.getStatus() == 1) {
                List<FireAlarmBean.DataBean> data = dataBean.getData();
                if (data != null && data.size() > 0) {
                    dataList.addAll(data);
                }
                if (data.size() < 10) {
                    xrv_fa.setLoadingMoreEnabled(false);
                }
                adapter.notifyDataSetChanged();
            }
        } catch (Exception E) {
        }
    }



}
